package cn.ztydata.teachingsystem.heaven.service;

import cn.ztydata.teachingsystem.common.Page;
import cn.ztydata.teachingsystem.exception.ServiceException;
import cn.ztydata.teachingsystem.heaven.common.WebContext;
import cn.ztydata.teachingsystem.heaven.cons.Roles;
import cn.ztydata.teachingsystem.heaven.cons.TeacherTeamRole;
import cn.ztydata.teachingsystem.heaven.dao.TeacherTeamDao;
import cn.ztydata.teachingsystem.heaven.entity.Teacher;
import cn.ztydata.teachingsystem.heaven.entity.TeacherTeam;
import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import com.google.common.collect.Sets;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import java.util.Set;

/**
 * 教师团队服务类
 * Created by chenxin on 2014/12/3.
 */
@Service
public class TeacherTeamService extends BaseService {
    @Autowired
    private TeacherTeamDao teacherTeamDao;

    @Autowired
    private TeacherService teacherService;

    @Autowired
    private WebContext webContext;

    /**
     * 获取指定教师的任课总数
     * @param teacherIdList 教师ID集合
     * @return Map
     *
     * @author cx
     * @since 2014-12-03
     */
    public Map<Long, Long> getCourseTotal(final List<Long> teacherIdList) {
        //获取教师的任课总数
        List<Map<String, Object>> courseTotalList = teacherTeamDao.getCourseTotal(teacherIdList);

        //将统计信息转换为“教师ID->课程总量”格式
        Map<Long, Long> teachingCourse = Maps.newHashMap();
        log.debug("循环转换教师任课统计格式");
        for (Map<String, Object> courseTotal : courseTotalList) {
            teachingCourse.put((Long)courseTotal.get("teacherId"), (Long)courseTotal.get("total"));
        }

        log.debug("return 教师任课统计map");
        return teachingCourse;
    }

    /**
     * 获取指定课程的教师团队信息
     * @param courseIdList 课程ID集合
     * @return Map
     *
     * @author cx
     * @since 2014-12-11
     */
    public Map<Long, List<TeacherTeam>> getTeams(List<Long> courseIdList) {

        if (courseIdList == null || courseIdList.isEmpty()) {
            log.debug("课程ID集合不为空：courseIdList == null || courseIdList.isEmpty()");
            return Maps.newHashMap();
        }

        //获取指定课程的教师团队信息
        List<TeacherTeam> memberList = teacherTeamDao.getAllForTeam(courseIdList);
        Map<Long, List<TeacherTeam>> result = Maps.newHashMap();
        //将信息转换为“课程ID->团队成员”格式
        log.debug("将信息转换为“课程ID->团队成员”格式:for (Long courseId : courseIdList) {");
        for (Long courseId : courseIdList) {

            List<TeacherTeam> team = Lists.newArrayList();

            log.debug("for (TeacherTeam member : memberList) {");
            for (TeacherTeam member : memberList) {
                if (courseId == member.getCourseId()) {
                    log.debug("如果课程ＩＤ等于member.getCourseId()：courseId == member.getCourseId()");
                    team.add(member);
                }
            }

            result.put(courseId, team);
        }

        log.debug("获取指定课程的教师团队信息");
        return result;
    }

    /**
     * 新增教师团队成员
     * @param teacherTeam 教师团队
     *
     * @author cx
     * @since 2014-12-13
     */
    public void saveMemeber(TeacherTeam teacherTeam) {
        //判断用户是否为课程负责人
        isLeader(teacherTeam.getCourseId());

        if (!teacherTeamDao.save(teacherTeam)) {
            log.debug("保存新增团队成员失败:!teacherTeamDao.save(teacherTeam)");
            log.warn("新增保存教师团队失败:courseId->" + teacherTeam.getCourseId() + ",teacherId->" + teacherTeam.getTeacherId());
            throw new ServiceException();
        }
    }

    /**
     * 移除教师团队成员
     * @param teacherId 教师ID
     * @param courseId 课程ID
     *
     * @author cx
     * @since 2014-12-13
     */
    public void deleteMember(final long teacherId, final long courseId) {
        //判断用户是否为课程负责人
        isLeader(courseId);

        if (!teacherTeamDao.delete(courseId, teacherId)) {
            log.debug("remove memeber failed:!teacherTeamDao.delete(courseId, teacherId)");

            log.warn("删除教师团队成员失败:courseId->" + courseId + ",teacherId->" + teacherId);
            throw new ServiceException();
        }
    }

    /**
     * 获取分页教师团队信息
     * @param courseId 课程ID
     * @param pageNum 页码
     * @param pageSize 每页条数
     * @return Page
     *
     * @author cx
     * @since 2014-12-13
     */
    public Page<TeacherTeam> getList(final long courseId, final int pageNum, final int pageSize) {
        //获取教师团队的分页数据
        Page<TeacherTeam> teamPage = teacherTeamDao.getTeachers(courseId, pageNum, pageSize);

        //获取教师的信息
        Set<Long> teacherIds = Sets.newHashSet();
        log.debug("循环教师集合");
        //获取教师ID集合
        for (TeacherTeam teacherTeam : teamPage.getItems()) {
            teacherIds.add(teacherTeam.getTeacherId());
        }

        Map<Long, Teacher> teacherInfos = teacherService.getAllForTeam(teacherIds);

        log.debug("循环绑定教师信息");
        //绑定教师信息
        for (TeacherTeam teacherTeam : teamPage.getItems()) {
            teacherTeam.setTeacherInfo(teacherInfos.get(teacherTeam.getTeacherId()));
        }

        log.debug("return teacherteam page list");
        return teamPage;
    }

    /**
     * 获取教师团队头像
     * @param courseId 课程ID
     * @return List
     *
     * @author cx
     * @since 2014-12-15
     */
    public List<TeacherTeam> getListForPic(final long courseId) {
        //获取教师团队数据
        List<TeacherTeam> teacherTeamList = teacherTeamDao.getTeachersForRole(courseId);

        //获取教师的姓名与头像
        Set<Long> teacherIds = Sets.newHashSet();
        log.debug("循环教师团队集合");
        //获取教师ID集合
        for (TeacherTeam teacherTeam : teacherTeamList) {
            teacherIds.add(teacherTeam.getTeacherId());
        }

        Map<Long, Teacher> teacherMap = teacherService.getPictures(teacherIds);

        log.debug("循环绑定教师信息");
        //绑定教师信息
        for (TeacherTeam teacherTeam : teacherTeamList) {
            teacherTeam.setTeacherInfo(teacherMap.get(teacherTeam.getTeacherId()));
        }

        log.debug("return 教师团队List");
        return teacherTeamList;
    }

    /**
     * 检查登录用户是否为教师团队成员
     *
     * @author chenxin
     * @since 2014/12/30
     */
    public void isMemeber(final long courseId) {
        //获取教师团队成员信息
        TeacherTeam teacherTeam = teacherTeamDao.getOne(courseId, webContext.getUserId());

        if (teacherTeam == null) {
            log.debug("非教师团队成员:teacherTeam == null");
            log.warn("非教师团队成员:courseId->{},teacherId->{}", courseId, webContext.getUserId());

            throw new ServiceException("非教师团队成员");
        }
    }

    /**
     * 检查登录用户是否为课程负责人
     *
     * @author chenxin
     * @since 2014/12/30
     */
    public void isLeader(final long courseId) {
        //获取登录用户在教师团队成员信息
        TeacherTeam teacherTeam = teacherTeamDao.getOne(courseId, webContext.getUserId());

        if (teacherTeam == null || (teacherTeam.getRole() != TeacherTeamRole.COURSE_LEADER && webContext.getRole() != Roles.SUPER_ADMIN)) {
            log.debug("非课程负责人:teacherTeam == null || (teacherTeam.getRole() != 0 && webContext.getRole() != Roles.SUPER_ADMIN)");
            log.warn("非课程负责人:courseId->{},teacherId->{}", courseId, webContext.getUserId());

            throw new ServiceException("非课程负责人");
        }
    }
}
